<template>
	<globalModal title="导入试题标准答案" :visible="visible" :width="400" :bodyStyle="bodyStyle"
		:confirmLoading="confirmLoading" @ok="handleOk" @cancel="handleCancel">
		<a-spin :spinning="confirmLoading">
			<a-form :form="form" autocomplete="off" :labelCol="labelCol" :wrapperCol="wrapperCol">
				<a-form-item label="上传">
					<div class="flex flex-pack-justify flex-align-top">
						<div class="flex-1">
							<a-upload name="file" :beforeUpload="beforeUpload" :showUploadList="false"
								accept="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
								v-decorator="['file', { rules: [{ required: true, message: '上传文件' }], valuePropName: 'file', getValueFromEvent: normFile }]">
								<a-button>
									<a-icon type="upload" />
									上传文件
								</a-button>
							</a-upload>
							<div>{{ filename }}</div>
						</div>
						<a style="margin-top: 4px;"
							:href="'/template/answer-template.xlsx?time=' + new Date().getTime()"
							class="ant-btn ant-btn-primary" download="试题标准答案模板.xlsx" @click="download">
							<a-icon type="download" />
							<span>模板下载</span>
						</a>
					</div>
				</a-form-item>
			</a-form>
		</a-spin>
	</globalModal>
</template>

<script>
	import { mapState, mapMutations } from 'vuex';
	import { postActionExport, sysManageapi, postActionUpload } from '@/api/index.js';
	import {
		ModalMixins
	} from '@/mixins';
	export default {
		name: 'importAnswerModal',
		mixins: [ModalMixins],
		data() {
			return {
				bodyStyle: {
					maxHeight: '450px',
					minHeight: '100px',
					overflow: 'auto'
				},
				labelCol: {
					xs: {
						span: 24
					},
					sm: {
						span: 6
					}
				},
				wrapperCol: {
					xs: {
						span: 24
					},
					sm: {
						span: 18
					}
				},
				filename:'',
				testPagerType:0,//默认是套题（随堂检测和课后作业），1是临时题
			}
		},
		methods: {
			...mapMutations(['SET_isNewTemplate']),
			show(record,status=0){
				this.mdl={...record};
				console.log("有没有校区"+JSON.stringify(this.mdl))
				console.log("有没有isAdmin"+JSON.stringify(this.isAdmin))
				this.testPagerType=status;
				this.visible=true;
			},
			
			beforeUpload(file) {
				return false;
			},
			normFile(e) {
				if (Array.isArray(e)) {
					return e;
				}
				this.filename = e.file.name;
				return e && e.file;
			},
			handleCancel(){
				this.visible=false;
				this.filename='';
				this.form.resetFields();
			},
			//提交保存数据
			handleOk() {
				// 触发表单验证
				this.form.validateFields((err, values) => {
					// 验证表单没错误
					if (!err) {
						this.confirmLoading = true;
						this.importFile(values);
					}
				});
			},
			importFile(values) {
				this.confirmLoading = true;
				const formData = new FormData();
				formData.append('file', values.file);
				formData.append('title', this.mdl.title);
				formData.append('subjItemCode', this.mdl.subjItemCode);
				formData.append('gradeId', this.mdl.gradeId);
				formData.append('classTechIds', this.mdl.classTechIds&&this.mdl.classTechIds.length>0?this.mdl.classTechIds:[]);
				if(this.mdl.classShareIds){
					formData.append('classShareIds', this.mdl.classShareIds);
				}
				if(this.mdl.testQuestId){
					formData.append('testQuestId', this.mdl.testQuestId);
				}
				if(this.mdl.testTime||this.mdl.testTime==0){
					formData.append('testTime', this.mdl.testTime);
				}
				formData.append('testType', this.mdl.testType);
				formData.append('isShare', this.mdl.isShare);
				formData.append('gradeName', this.mdl.gradeName);
				formData.append('subjectName', this.mdl.subjectName);
				//默认是套题
				formData.append('status', this.testPagerType);
				//if(this.isAdmin){
					formData.append('deptId', this.mdl.deptId);
				//}
				if(this.testPagerType==1){
					formData.append('classId', this.mdl.classId);
				}
				postActionUpload(sysManageapi.testquestUrl.importAnswer, formData)
					.then(res => {
						if (res.code == 1) {
							this.confirmLoading = false;
							this.$message.success('导入成功');
							this.$emit('ok',res.data);
							this.handleCancel();
						}
					})
					.finally(() => {
						this.confirmLoading = false;
					});
			},
			download(){
				this.SET_isNewTemplate('0')
			}
		}
	}
</script>

<style>
</style>
